package com.eight.cloud.mybatisplus.annotation.enums;

import com.eight.cloud.mybatisplus.datascope.SqlWhereExpression;
import com.eight.cloud.mybatisplus.datascope.expression.DeptExpression;
import com.eight.cloud.mybatisplus.datascope.expression.UserSelfExpression;
import lombok.Getter;

import java.util.Arrays;

/**
 * 数据权限类型
 *
 * @EnumName: DataScopeType
 * @Author: TXC
 * @Date: 2025-06-11 17:36
 **/
@Getter
public enum DataScopeType {
    DEPT("dept", new DeptExpression()),
    SELF("self", new UserSelfExpression());

    final String code;
    final SqlWhereExpression sqlWhereExpression;

    DataScopeType(String code, SqlWhereExpression sqlWhereExpression) {
        this.code = code;
        this.sqlWhereExpression = sqlWhereExpression;
    }

    public static SqlWhereExpression getExpression(String code) {
        return Arrays.stream(values())
                .filter(s -> s.getCode().equals(code))
                .findFirst()
                .map(DataScopeType::getSqlWhereExpression)
                .orElseThrow(() -> new IllegalArgumentException("无效策略: " + code));
    }
}
